<?php

if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 高级版专享函数库（请勿复制与转载）
 *
 * @since		version 2.3.3
 * @author		Dayrui <dayrui@gmail.com>
 * @license     http://www.dayrui.com/license
 * @copyright   Copyright (c) 2011 - 9999, Dayrui.Com, Inc.
 */

/**
 * 邮箱或手机号码登录
 *
 * @param	string	$dir	目录名称
 * @return	bool|void
 */
function dr_vip_login($db, $value) {

    if (preg_match('/^[\w\-\.]+@[\w\-\.]+(\.\w+)+$/', $value)) {
        // 邮箱登录
        return $db->select('`uid`, `password`, `salt`, `email`, `username`')
                  ->where('email', $value)
                  ->limit(1)
                  ->get('member')
                  ->row_array();
    } else {
        // 手机登录
        $phone = (int)$value;
        if (strlen($phone) == 11) {
            return $db->select('`uid`, `password`, `salt`, `email`, `username`')
                      ->where('phone', $phone)
                      ->limit(1)
                      ->get('member')
                      ->row_array();
        }
        return NULL;
    }

}

/**
 * 获取站点表单内容函数
 *
 * @param	intval	$id 	表单id
 * @param	string	$form	表单表名称
 * @param	string	$field	显示字段，默认为全部数组
 * @param	intval	$sid	站点id，默认为当前站点
 * @param	intval	$cache	缓存时间，默认为10000秒
 * @return	array|void
 */
function dr_vip_form($id, $form, $field = 0, $sid = 0, $cache = 0) {

    $ci = &get_instance();
    $sid = $sid ? $sid : SITE_ID;
    $name = 'form-data-'.$sid.'-'.$form.'-'.$id;
    $data = $ci->get_cache_data($name);
    if (!$data) {
        $data = $ci->site[$sid]->where('id', $id)->get($sid.'_form_'.$form)->row_array();
        $data2 = $ci->site[$sid]->where('id', $id)->get($sid.'_form_'.$form.'_data_'.(int)$data['tableid'])->row_array();
        if ($data2) {
            $data = array_merge($data, $data2);
        }
        if (!$data) {
            return false;
        }
        $ci->set_cache_data($name, $data, $cache ? $cache : SYS_CACHE_FORM);
    }

    return $field && isset($data[$field]) ? $data[$field] : $data;

}

/**
 * 获取模块表单内容函数
 *
 * @param	intval	$id 	表单id
 * @param	string	$dir	模块目录
 * @param	string	$form	表单表名称
 * @param	string	$field	显示字段，默认为全部数组
 * @param	intval	$sid	站点id，默认为当前站点
 * @param	intval	$cache	缓存时间，默认为10000秒
 * @return	array|void
 */
function dr_vip_mform($id, $dir, $form, $field = 0, $sid = 0, $cache = 0) {

    $ci = &get_instance();
    $sid = $sid ? $sid : SITE_ID;
    $name = 'mform-data-'.$sid.'-'.$form.'-'.$id.'-'.$dir;
    $data = $ci->get_cache_data($name);
    if (!$data) {
        $data = $ci->site[$sid]->where('id', $id)->get($sid.'_'.$dir.'_form_'.$form)->row_array();
        $data2 = $ci->site[$sid]->where('id', $id)->get($sid.'_'.$dir.'_form_'.$form.'_data_'.(int)$data['tableid'])->row_array();
        if ($data2) {
            $data = array_merge($data, $data2);
        }
        if (!$data) {
            return false;
        }
        $ci->set_cache_data($name, $data, $cache ? $cache : SYS_CACHE_FORM);
    }

    return $field && isset($data[$field]) ? $data[$field] : $data;

}

// 获取栏目数据及自定义字段
function dr_cat_value() {
	
	$get = func_get_args();
    if (empty($get)) {
        return NULL;
    }
	
	if (is_numeric($get[0]) && APP_DIR) {
		// 值是栏目id时，表示当前模块
		$name = 'module-'.SITE_ID.'-'.APP_DIR;
	} else {
		// 指定模块
		$name = strpos($get[0], '-') ? 'module-'.$get[0] : 'module-'.SITE_ID.'-'.$get[0];
		unset($get[0]);
	}
	
	$i = 0;
	$param = array();
	foreach ($get as $t) {
		if ($i == 0) {
			$param[] = $name;
			$param[] = 'category';
		}
		$param[] = $t;
		$i = 1;
	}
	$ci = &get_instance();
	
	return call_user_func_array(array($ci, 'get_cache'), $param);
}

// 搜索商城属性
function dr_mall_property($catid, $all = 0) {
	
	if (!is_dir(FCPATH.'mall/')) {
		return NULL;
	}
	
	$ci = &get_instance();
	$ci->load->add_package_path(FCPATH.'mall/');
	$ci->load->model('property_model');
	$data = $ci->property_model->get_cat_data($catid);
	if (!$data) {
		return NULL;
	}

    if ($all) {
        return $data;
    }

	$list = array();
	foreach ($data as $t) {
		if ($t['list']) {
			foreach ($t['list'] as $a) {
				if ($a['search']) {
					$list[$a['cname']] = $a;
				}
			}
		}
	}
	
	return $list;
}

function dr_mall_property_value($catid, $value) {

    if (!$catid || !$value) {
        return NULL;
    }

    $data = dr_mall_property($catid, 1);
    if (!$data) {
        return NULL;
    }

    $list = array();
    $params = array('catid' => $catid);
    foreach ($data as $t) {
        if ($t['list']) {
            $i = $t['name'];
            $list[$i] = array();
            foreach ($t['list'] as $a) {
                if (!isset($value[$a['cname']]) || !$value[$a['cname']]) {
                    continue;
                }
                if ($a['type'] == 'text') {
                    $row = $value[$a['cname']];
                } elseif ($a['type'] == 'select') {
                    $row = $a['value'][$value[$a['cname']]];
                    if ($a['search']) {
                       $row = '<a href="'.dr_search_url($params, 'mall_'.$a['cname'], $value[$a['cname']]).'" target="_blank" class="dr_pt">'.$row.'</a>';
                    }
                } else {
                    $row = array();
                    foreach ($value[$a['cname']] as $v) {
                        if ($a['search']) {
                            $row[$v] = '<a href="'.dr_search_url($params, 'mall_'.$a['cname'], $v).'" target="_blank" class="dr_pt">'.$a['value'][$v].'</a>';
                        } else {
                            $row[$v] = $a['value'][$v];
                        }
                    }
                }
                $list[$i][$a['name']] = $row;
            }
        }
    }

    return $list;
}

// 调用商城商品规格
function dr_mall_spec($catid, $value = array()) {

    if (!$catid) {
        return NULL;
    }

    if (!is_dir(FCPATH.'mall/')) {
        return NULL;
    }

    $ci = &get_instance();
    $ci->load->add_package_path(FCPATH.'mall/');
    $ci->load->model('spec_model');
    $data = $ci->spec_model->get_cat_data($catid);
    if (!$data && !$value) {
        return NULL;
    }

    if (!$value) {
        return $data;
    }

    if (isset($value['diy_group']) && $value['diy_group']) {
        foreach ($value['option'] as $cname => $v) {
            if (strpos($cname, 'group') === 0
                && isset($value['diy_group'][$cname]) && $value['diy_group'][$cname] ) {
                // 如果查询到这个值是自定义的值就补充到属性中
                $diy_value = array();
                foreach ($v as $a) {
                    $div_value[$a] = $value['diy_value'][$a];
                }
                $data[$cname] = array(
                    'type' => 'diy',
                    'name' => $value['diy_group'][$cname],
                    'cname' => $cname,
                    'value' => $diy_value,
                );
            }
        }
    }

    if (isset($value['diy_value']) && $value['diy_value']) {
        foreach ($value['option'] as $cname => $t) {
            foreach ($t as $val) {
                if (strpos($val, 'value') === 0
                    && isset($value['diy_value'][$val]) && $value['diy_value'][$val]
                    && isset($data[$cname]) && $data[$cname] ) {
                    // 如果查询到这个值是自定义的值就补充到属性中
                    $data[$cname]['value'][$val] = $value['diy_value'][$val];
                }
            }
        }
    }

    return $data;
}
